
import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 



import pluginUtils.DEMReader;
import pluginUtils.Point2D;

public class DrawingSlice extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	
	
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
	
		double limit=0.6; 
		double lmoins=-0.6;
		
		
	
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		
		/*
		setBounds(0,1201,0,1201);
		setCoefs(); 
		*/
		// background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		show();
		setProcessor("Verifying", primeIP);
		show(); 
		primeIP.setLineWidth(3);
		
		DEMReader toto=new DEMReader(); 
		toto.afficheFichierTexte("C:/Users/decomite/Downloads/grand_canyon-w/grand_canyon-w");
		//int[][] values=toto.getValeurs(); 
		
		// ici toutes les donnees ont ete lues
		System.out.println("min : "+toto.getMinElevationInt()+" max "+toto.getMaxElevationInt()); 
		int min=toto.getMinElevationInt(); 
		int max=toto.getMaxElevationInt(); 
		
		setBounds(0,1201,min,max); 
		setCoefs();
		for(int k=0;k<1201;k++){
			primeIP.setColor(Color.WHITE);
			primeIP.fillPolygon(background);
			primeIP.setColor(Color.BLACK);
		int values[]=toto.getSlice(k); 
		this.setTitle(k+"");
		for(int i=0;i<values.length-1;i++) { drawLine(i,values[i],i+1,values[i+1]);} 
		updateAndDraw();
		}
	}	
}
